<?php
declare (strict_types = 1);

namespace app\middleware;

use app\model\Auth as AuthModel;

class Auth
{
    private $toast = 'public/toast';

    /**
     * 处理请求
     *
     */
    public function handle($request, \Closure $next)
    {
        //得到管理员
        $auth = AuthModel::where('name', session('admin'))->find();
        //权限模块列表
        $roles = [];
        //遍历角色列表
        foreach ($auth->role as $key => $item) {
            foreach (explode(',', $item->uri) as $item) {
                $roles[] = $item;
            }
        }
        //得到当前的uri
        $uri = $request->controller() . '/' . $request->action();

        //超管判断
        if ($roles[0] != 'All') {
            //权限范围 提示
            if (!in_array($uri, $roles)) {
                return view($this->toast, [
                    'infos' => ['你没有操作权限'],
                    'url_text' => '返回首页',
                    'url_path' => url('/')
                ]);
            }
        }


        return $next($request);
    }
}
